/*
 * The MIT License
 *
 * Copyright (c) 2009 André D. Piske
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */
package org.gohtml.tree;

/**
 * @author André D. Piske
 * @version 0.1 alpha
 * @since 0.1 alpha
 */
public final class StringUtil {

    public static final int READTEXT_INCLUDE_QUOTES = 0x01;
    public static final int READTEXT_OR_CHARS = 0x02;
    //
    private String textRead;
    private int readPosition;
    private boolean reachEOF;
    private char delimiter;

    private StringUtil() {
    }

    public static StringUtil readText(String text, String end, int options) {
        return readText(text, end, options, 0);
    }

    public static StringUtil readText(String text, String end, int options, int offset) {
        StringUtil result = new StringUtil();
        int length = text.length();
        StringBuilder buffer = new StringBuilder(length);

        result.reachEOF = true;

        final int endLength = end.length();
        int endCount = 0;
        char quote = 0;
        int p = offset;
        for (; p < length; ++p) {
            char c = text.charAt(p);
            if (c == '"' || c == '\'') {
                if (quote == c) {
                    quote = 0;
                } else {
                    quote = c;
                }
                if ((options & READTEXT_INCLUDE_QUOTES) != 0) {
                    buffer.append(c);
                }
            } else {
                if (c == ':' && p + 1 < length) {
                    ++p;
                    c = text.charAt(p);
                    buffer.append(c);
                    endCount = 0;
                } else {
                    if (0 != (options & READTEXT_OR_CHARS)) {
                        if (end.indexOf(c) != -1) {
                            result.reachEOF = false;
                            result.textRead = buffer.toString();
                            result.delimiter = c;
                            result.readPosition = p + 1;
                            break;
                        }
                    } else {
                        if (quote == 0 && end.charAt(endCount) == c) {
                            endCount++;
                        } else {
                            endCount = 0;
                        }
                        if (endCount == endLength) {
                            result.reachEOF = false;
                            result.textRead = buffer.substring(0, buffer.length() - endCount + 1);
                            result.readPosition = p + endCount;
                            break;
                        }
                    }
                    buffer.append(c);
                }
            }
        }

        if (result.textRead == null) {
            result.textRead = buffer.toString();
        }
        if (result.reachEOF) {
            result.readPosition = p;
        }

        return result;
    }

    public char getDelimiter() {
        return delimiter;
    }

    public boolean reachEOF() {
        return reachEOF;
    }

    public String getText() {
        return textRead;
    }

    public int getPosition() {
        return readPosition;
    }
}
